import $service from '../../../../service.js';
var id;
let type;
let value;
const app = getApp();
Page({
    data:{
       active : true,
       imgUrl: ''
    },
    onLoad(e){
      id = e.id
      type = e.type
      value = e.value
      this.getEstablishPdf()
    },
    getEstablishPdf() {
      let params = {
        "id": id,
        "resourceType": type,
        "type": 2,
        "template": value
      }
      app.http.getEstablishPdf(params, true).then(res => {
        this.setData({
          imgUrl: res.data.url
        })
      }).catch(err => {
        $service.showToast(err.msg);
      })
    },
    previewImage: function(e) { //图片放大
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.num;
        var all = [];
        all.push(this.data.imgUrl);
        all.forEach(item => {
            item = item.replace(/https/, 'http')
        })
        wx.previewImage({
            urls: all
        })
    },
    guidanceMapTap(e){
        var num = e.currentTarget.dataset.num;
        if(num == 1){
            var imgSrc = this.data.imgUrl;
            if(!$service.isUndefinedAndEmpty(imgSrc)){
                wx.downloadFile({
                  url: imgSrc,
                  success: function (res) {
                    console.log(res);
                    //图片保存到本地
                    wx.saveImageToPhotosAlbum({
                      filePath: res.tempFilePath,
                      success: function (data) {
                        wx.showToast({
                          title: '保存成功',
                          icon: 'success',
                          duration: 2000
                        })
                      },
                      fail: function (err) {
                        console.log(err);
                        if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                          console.log("当初用户拒绝，再次发起授权")
                          wx.openSetting({
                            success(settingdata) {
                              console.log(settingdata)
                              if (settingdata.authSetting['scope.writePhotosAlbum']) {
                                console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                              } else {
                                console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                              }
                            }
                          })
                        }
                      },
                      complete(res){
                        console.log(res);
                      }
                    })
                  }
                })
            }else{
                $service.showToast('未生成成功')
            }
        }else{
            wx.navigateBack({
                delta:1
            })
        }
        
            
    }
})